package com.shaoshaohuo.app.ui;

import java.util.Arrays;
import java.util.List;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.adapter.FaceMarketHeadVpAdapter;
import com.shaoshaohuo.app.view.CirclePageIndicator;
import com.shaoshaohuo.app.view.PhotoViewPager;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.ImageLoadUtil;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;

/**
 * @author: yusonglin
 * @类 说 明:
 * @version 1.0
 * @创建时间：2015年8月24日 上午11:13:15
 * 
 */
public class PhotoBrowseActivity extends BaseActivity {

    private TopbarView mTopbarView;

    private PhotoViewPager mViewPager;
    private CirclePageIndicator mCirclePageIndicator;

    private List<String> photoArray;

    private int photoIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_browse);

        initData();
        initView();
        setUpView();
    }

    private void initData() {
        Intent intent = getIntent();
        String[] stringArrayExtra = intent.getStringArrayExtra(ExtraName.photoArray);
        photoIndex = intent.getIntExtra(ExtraName.photoIndex, 0);
        if (stringArrayExtra == null) {
            finish();
        }else{
            photoArray = Arrays.asList(stringArrayExtra);
        }
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

        mViewPager = (PhotoViewPager) this.findViewById(R.id.view_pager);
        mCirclePageIndicator = (CirclePageIndicator) this.findViewById(R.id.cpi_face_viewpager);

    }

    private void setUpView() {
        mTopbarView.setCenterText("");
        mTopbarView.setLeftView(true, true);
        mTopbarView.setVisibility(View.GONE);

        HeaderVpAdapter adapter = new HeaderVpAdapter(this, photoArray, 1, photoArray.size());
        mViewPager.setAdapter(adapter);
        mCirclePageIndicator.setViewPager(mViewPager);
        mCirclePageIndicator.notifyDataSetChanged();
        mViewPager.setCurrentItem(photoIndex);
    }

    private class HeaderVpAdapter extends FaceMarketHeadVpAdapter<String> {

        public HeaderVpAdapter(Context context, List<String> array, int previewCount, int maxListSize) {
            super(context, array, previewCount, maxListSize);
        }

        @Override
        public Object getView(ViewGroup container, String data, int position) {
            PhotoView photoView = new PhotoView(container.getContext());

            ImageLoader.getInstance().displayImage(data, photoView, ImageLoadUtil.build);

            container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

            photoView.setOnPhotoTapListener(finishOnClickListener);
            
            return photoView;
        }

    }
    
    private OnPhotoTapListener finishOnClickListener = new OnPhotoTapListener() {
        
        @Override
        public void onPhotoTap(View arg0, float arg1, float arg2) {
            finish();
        }
    };

}
